﻿using System;
using System.Data.SqlClient;
using System.Collections.Generic;

using MVCFoto.IDAL;
using MVCFoto.Model;
using System.Data;

namespace MVCFoto.SQLServerDAL
{
    public class User : IUser
    {
        private const string PARM_USERID = "@UserID";
        private const string PARM_USERNAME = "@UserName";
        private const string PARM_PASSWORD = "@Password";
        private const string PARM_EMAIL = "@Email";
        private const string PARM_GENDER = "@Gender";
        private const string PARM_AVATAR = "@Avatar";
        private const string PARM_USERQUOTE = "@UserQuote";
        private const string PARM_PRIVACY_INFO = "@Privacy_Info";
        private const string PARM_REGDATE = "@RegDate";
        private const string PARM_LASTLOGIN = "@LastLogin";
        private const string PARM_REGIP = "@RegIP";
        private const string PARM_LASTIP = "@LastIP";
        private const string PARM_LOGINS = "@Logins";
        private const string PARM_USERQUOTA = "@UserQuota";

        public UserInfo GetUser(int id)
        {
            SqlParameter[] param = {			    
			    SqlHelper.MakeInParam(PARM_USERID,SqlDbType.Int,4,id)
                                    };
            UserInfo ui = null;
            using (SqlDataReader sdr = SqlHelper.ExecuteReader("mfp_Get_User_By_UserId", param))
            {
                IList<UserInfo> users = FetchUsersInfoFromReader(sdr);
                if (users.Count > 0)
                    ui = users[0];

            }
            
            return ui;       
        }
        public UserInfo GetUser(string username)
        {
             SqlParameter[] param = {			    
			    SqlHelper.MakeInParam(PARM_USERNAME,SqlDbType.VarChar,20,username)
                                    };
             UserInfo ui = null;
             using (SqlDataReader sdr = SqlHelper.ExecuteReader("mfp_Get_User_By_UserName", param))
             {
                 IList<UserInfo> users = FetchUsersInfoFromReader(sdr);
                 if (users.Count > 0)
                     ui = users[0];

             }
            return ui;
        }        
        
        public UserInfo GetUser(string username, string password)
        {
            SqlParameter[] param = {			    
			    SqlHelper.MakeInParam(PARM_USERNAME,SqlDbType.VarChar,20,username),
                 SqlHelper.MakeInParam(PARM_PASSWORD,SqlDbType.VarChar,64,password)
                                    };
            UserInfo ui = null;
            using (SqlDataReader sdr = SqlHelper.ExecuteReader("mfp_Get_User_By_UserNamePassword", param))
            {
                IList<UserInfo> users = FetchUsersInfoFromReader(sdr);
                if (users.Count > 0)
                    ui = users[0];

            }
            return ui;
        }

        public UserInfo GetUserByEmail(string email)
        {             
             SqlParameter[] param = {			    
			   SqlHelper.MakeInParam(PARM_EMAIL,SqlDbType.VarChar,255,email)
                                    };
            UserInfo ui=null;
            using (SqlDataReader sdr = SqlHelper.ExecuteReader("mfp_Get_User_By_Email", param))
            {
                IList<UserInfo> users = FetchUsersInfoFromReader(sdr);
                if (users.Count > 0)
                    ui = users[0];

            }
            return ui;
        }   

        public int AddUser(UserInfo ui)
        {
            SqlParameter[] param = {			    
			    SqlHelper.MakeInParam(PARM_USERNAME,SqlDbType.VarChar,20,ui.UserName),	
			    SqlHelper.MakeInParam(PARM_PASSWORD,SqlDbType.VarChar,64,ui.Password),	
			    SqlHelper.MakeInParam(PARM_EMAIL,SqlDbType.VarChar,255,ui.Email),	
			    SqlHelper.MakeInParam(PARM_GENDER,SqlDbType.SmallInt,2,ui.Gender),	
			    SqlHelper.MakeInParam(PARM_AVATAR,SqlDbType.VarChar,255,ui.Avatar),	
			    SqlHelper.MakeInParam(PARM_USERQUOTE,SqlDbType.VarChar,255,ui.UserQuote),	
			    SqlHelper.MakeInParam(PARM_PRIVACY_INFO,SqlDbType.SmallInt,2,ui.Privacy_Info),	
			    SqlHelper.MakeInParam(PARM_REGDATE,SqlDbType.DateTime,0,ui.RegDate),			
			    SqlHelper.MakeInParam(PARM_REGIP,SqlDbType.VarChar,64,ui.RegIP),
                SqlHelper.MakeInParam(PARM_USERQUOTA,SqlDbType.BigInt,8,ui.UserQuota)	
               };
            return (int)SqlHelper.ExecuteOneValue("mfp_Insert_User", param);
        }
        public bool UpdateUser(UserInfo ui)
        {
            SqlParameter[] param = {	
		         SqlHelper.MakeInParam(PARM_USERID,SqlDbType.Int,4,ui.UserID),
			    SqlHelper.MakeInParam(PARM_USERNAME,SqlDbType.VarChar,20,ui.UserName),	
			   // SqlHelper.MakeInParam(PARM_PASSWORD,SqlDbType.VarChar,64,ui.Password),	
			    SqlHelper.MakeInParam(PARM_EMAIL,SqlDbType.VarChar,255,ui.Email),	
			    SqlHelper.MakeInParam(PARM_GENDER,SqlDbType.SmallInt,2,ui.Gender),	
			    SqlHelper.MakeInParam(PARM_AVATAR,SqlDbType.VarChar,255,ui.Avatar),	
			    SqlHelper.MakeInParam(PARM_USERQUOTE,SqlDbType.VarChar,255,ui.UserQuote),	
			    SqlHelper.MakeInParam(PARM_PRIVACY_INFO,SqlDbType.SmallInt,2,ui.Privacy_Info),	
			   SqlHelper.MakeInParam(PARM_LASTLOGIN,SqlDbType.DateTime,0,ui.LastLogin),			
			    SqlHelper.MakeInParam(PARM_LASTIP,SqlDbType.VarChar,64,ui.LastIP),	
			    SqlHelper.MakeInParam(PARM_LOGINS,SqlDbType.Int,4,ui.Logins)	
               };
            return SqlHelper.ExecuteNonQuery("mfp_Update_User", param) > 0 ? true : false;
        }
        public bool ChangePassword(int userId, string password)
        {
            SqlParameter[] param = {	
		         SqlHelper.MakeInParam(PARM_USERID,SqlDbType.Int,4,userId),			  
			    SqlHelper.MakeInParam(PARM_PASSWORD,SqlDbType.VarChar,64,password)
			    
               };
            return SqlHelper.ExecuteNonQuery("mfp_Update_User_Password", param) > 0 ? true : false;
        }
        public IList<UserInfo> GetUserFriends(int userId)
        {
            //
            SqlParameter[] param = {			    
			    SqlHelper.MakeInParam(PARM_USERID,SqlDbType.Int,4,userId)
                                    };
            IList<UserInfo> users = null;
            using (SqlDataReader sdr = SqlHelper.ExecuteReader("mfp_Get_User_Friends", param))
            {
                users = FetchUsersInfoFromReader(sdr);
            }
            return users;
        }
        IList<UserInfo> FetchUsersInfoFromReader(SqlDataReader sdr)
        {
            IList<UserInfo> users = new List<UserInfo>();
            while (sdr.Read())
            {
                UserInfo ui = new UserInfo();
                ui.UserID = sdr.GetInt32(0);
                ui.UserName = sdr.GetString(1);
                ui.Password = sdr.GetString(2);
                ui.Email = sdr.GetString(3);
                ui.Gender = sdr.IsDBNull(4) ? (short)0: sdr.GetInt16(4);
                ui.Avatar = sdr.IsDBNull(5) ? "" : sdr.GetString(5);
                ui.UserQuote = sdr.IsDBNull(6) ? "" : sdr.GetString(6);
                ui.Privacy_Info = sdr.IsDBNull(7) ? 0 : sdr.GetInt16(7);
                ui.RegDate = sdr.IsDBNull(8) ? DateTime.Now : sdr.GetDateTime(8);
                ui.LastLogin = sdr.IsDBNull(9) ? DateTime.Now : sdr.GetDateTime(9);
                ui.RegIP = sdr.IsDBNull(10) ? "" : sdr.GetString(10);
                ui.LastIP = sdr.IsDBNull(11) ? "" : sdr.GetString(11);
                ui.Logins = sdr.GetInt32(12);
                ui.UserQuota = sdr.IsDBNull(13) ? 0 : sdr.GetInt64(13);
                ui.QuotaUsed = sdr.IsDBNull(14) ? 0 : sdr.GetInt64(14);
                users.Add(ui);
            }
            return users;
        }
    }
}
